import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Gui extends JFrame{

	ArrayList <JPanel> walkBoxesG = new ArrayList<JPanel>();
	ArrayList <JPanel> startPosG = new ArrayList<JPanel>();
	JLabel[] picLabels = new JLabel[4];
	JTextArea textarea;
	JPanel lpButtonSpaceDice = new JPanel(new BorderLayout());
	
	// GUI constructor
	public Gui(){
	super("CLOBRDA OMG WTF");
	// pozadi
	BufferedImage backgroundimg=null;
	try {
		backgroundimg = ImageIO.read(new File("./src/images/wood"+".jpg"));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	BackgroundPanel panel = new BackgroundPanel(backgroundimg, BackgroundPanel.ACTUAL, 1.0f, 0.5f);
	
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	setSize(800,600);
	setLocationRelativeTo(null);
	setLayout(new BorderLayout());
	
	// NEW Grid object which paints the grid itself
	Grid grid = new Grid();
	//NEW Turn object
	NewTurn turn = new NewTurn();
	
	//GUI components
	JPanel leftPanel = new JPanel(new GridLayout(0,1));
	JPanel lpButtonSpace = new JPanel(new BorderLayout());
	JPanel figSpace = new JPanel(new GridLayout(2,2));
	JButton[] buttons = new JButton[4];
	for (int i=0; i<=3; i++){
		buttons[i] = new JButton("Figure " + (i+1));
	}
	JButton button = new JButton("ROLL THE DIE!");
	button.addActionListener(turn);
	textarea = new JTextArea(5,20);
	JScrollPane  jp = new JScrollPane(textarea);
	jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	textarea.setEditable(false);
	
	//Adding components
	add(leftPanel, BorderLayout.WEST);
	leftPanel.add(lpButtonSpace);
	leftPanel.add(figSpace);
	lpButtonSpace.add(button, BorderLayout.NORTH);
	lpButtonSpace.add(lpButtonSpaceDice, BorderLayout.CENTER);
	add(panel);
	panel.add(grid.getGrid(), BorderLayout.CENTER);
	leftPanel.add(jp);
	for (int i=0; i<=3; i++){
		figSpace.add(buttons[i]);
	}
	
	walkBoxesG.addAll(grid.walkBoxes);	
	startPosG.addAll(grid.startPos);
	fillJLabels();
	drawStart();
	setVisible(true);
	
	}
	
	// vytvoreni JLabelu
	public void fillJLabels (){
		String[] colors = {"red", "blue", "green", "yellow"};
		BufferedImage figure;
		
		for(int i = 0; i<=3; i++){
			try {
				figure = ImageIO.read(new File("./src/images//clobrda_"+colors[i]+".png"));
				picLabels[i] = new JLabel(new ImageIcon(figure));
			} catch (IOException e) {
				System.out.println("Image not found");
				e.printStackTrace();
			}	
			
			}
	}
	
	// METODA pro prekreslovani figurek
	public void redrawFigure(int numberThrown, int oldposition, Figure x) {
		lpButtonSpaceDice.removeAll();
		lpButtonSpaceDice.add(showDie(numberThrown), BorderLayout.CENTER);
		lpButtonSpaceDice.repaint();
		lpButtonSpaceDice.revalidate();
		
		
		textarea.append("\n"+ x.getColor() + " has thrown " + numberThrown + " new pos " + x.getPosition());
		int figureid=0; 
		
		if (x.getColor().equals("red")){
			redrawStart(0);
			figureid=0;
			}
		if (x.getColor().equals("blue")){
			redrawStart(1);
			figureid=1;
			}
		if (x.getColor().equals("green")){
			redrawStart(2);
			figureid=2;
			}
		if (x.getColor().equals("yellow")){
			redrawStart(3);
			figureid=3;
			}
		JLabel picLabel = picLabels[figureid];
		walkBoxesG.get(oldposition).removeAll();
		walkBoxesG.get(oldposition).repaint();
		walkBoxesG.get(oldposition).revalidate();
		walkBoxesG.get(x.getPosition()).add(picLabel, BorderLayout.CENTER);
		walkBoxesG.get(x.getPosition()).repaint();
		walkBoxesG.get(x.getPosition()).revalidate();
	}
	
	// vykresli pocatecni pozice
	public void drawStart(){
		for (int i=0;i<4;i++){
		startPosG.get(i).add(picLabels[i]);
		startPosG.get(i).repaint();
		startPosG.get(i).revalidate();
		}
		//JLabel picLabel=null;
		/*picLabel.addComponentListener(new java.awt.event.ComponentAdapter() {
			 public void componentResized(java.awt.event.ComponentEvent evt)
			 {
			 componentResized(evt);
			 }
			 });
		
	     */
		
		
	}
	
	//prekresleni zacatku
	public void redrawStart(int i){
		startPosG.get(i).removeAll();
		startPosG.get(i).repaint();
		startPosG.get(i).revalidate();
	}
	
	//prekresleni figurky pri vyhozeni
	public void redrawFigure(int numberThrown,int oldposition, Figure x, Figure y) {
		lpButtonSpaceDice.removeAll();
		lpButtonSpaceDice.add(showDie(numberThrown), BorderLayout.CENTER);
		lpButtonSpaceDice.repaint();
		lpButtonSpaceDice.revalidate();
		
		BufferedImage figure2;
		walkBoxesG.get(x.getPosition()).removeAll();
		walkBoxesG.get(x.getPosition()).repaint();
		walkBoxesG.get(x.getPosition()).revalidate();
		redrawFigure(numberThrown, oldposition, x);
		if (y.getColor().equals("red")){
			
			JLabel picLabel2 = picLabels[0];
			startPosG.get(0).add(picLabel2, BorderLayout.CENTER);
			startPosG.get(0).repaint();
			startPosG.get(0).revalidate();
			}
			
		if(y.getColor().equals("blue")){
				
				JLabel picLabel2 = picLabels[1];
				startPosG.get(1).add(picLabel2, BorderLayout.CENTER);
				startPosG.get(1).repaint();
				startPosG.get(1).revalidate();
			}
		if(y.getColor().equals("green")){
				JLabel picLabel2 = picLabels[2];
				startPosG.get(2).add(picLabel2, BorderLayout.CENTER);
				startPosG.get(2).repaint();
				startPosG.get(2).revalidate();
			}
		if(y.getColor().equals("yellow")){
				JLabel picLabel2 = picLabels[3];
				startPosG.get(3).add(picLabel2, BorderLayout.CENTER);
				startPosG.get(3).repaint();
				startPosG.get(3).revalidate();
			}
		textarea.append("\n"+ y.getColor() + " was thrown OUT");
	}
	
	public JLabel showDie(int i){
		BufferedImage die;
		JLabel dieLabel = null;
		try {
			die = ImageIO.read(new File("./src/images//dice"+i+".jpg"));
			dieLabel = new JLabel(new ImageIcon(die));
		} catch (IOException e) {
			System.out.println("die image not loaded!!!!!!");
			e.printStackTrace();
		}
		
		return dieLabel;
		
	}
}